#coding=utf-8
from uliweb import expose, decorators
import generic.views as g_views
from uliweb.core.html import Tag

from uliweb import expose
from sqlalchemy import *
from uliweb.orm import *
from sqlalchemy.sql import select

from uliweb import orm
import os

@decorators.require_login
@expose('/selfdevs/<id>/view')
def selfdevs_view(id):
    from uliweb.orm import get_model
        
    System = get_model('system')
    obj = System.get(int(id))
    Selfdevs = get_model('selfdev')
    objs = Selfdevs.get(Selfdevs.c.system == int(id))
    
    if not objs:
        objs = Selfdevs(system=obj, kfzx=obj.kfzx)
        objs.save()
    
#    if objs.qualified:
#        data = '是'
#    else:
#        data = '否'
    
#    return {'objects':objs, 'data':data}
    return {'objects':objs}

@decorators.require_login
@expose('/selfdevs/<id>/edit')
def selfdevs_edit(id):
    from uliweb.utils.generic import EditView
    from uliweb.orm import get_model
    from uliweb.form import DateField
    from plugs.ui.jquery.jquery.widgets import DatePicker
    from datetime import datetime, date
    from uliweb.form.template_layout import TemplateLayout
    
    System = get_model('system')
    obj = System.get(int(id))
    Selfdevs = get_model('selfdev')
    objs = Selfdevs.get(Selfdevs.c.system == int(id))
        
    data = {}
    data['system'] = objs.system.name
    data['kfzx'] = objs.kfzx.name
        
    static_fields = ('system', 'kfzx')
    fields = ('system', 'kfzx', 'qualified_year','qualified_type','complexity', 'independent_lastyear', 
        'independent_thisyear', 'independent_destination')
    
    def post_created_form(fcls, model, objs):
        fcls.layout_class = TemplateLayout
        fcls.layout = 'layout_selfdevs_edit.html'
        
    def pre_save(objs, data):
        for k in static_fields:
            del data[k]
        data['system'] = objs.system
        data['kfzx'] = objs.kfzx 
    
        
    view = EditView(Selfdevs, ok_url='/systems/%s' % id, obj=objs, data=data,
        post_created_form=post_created_form, fields=fields, 
        static_fields=static_fields, pre_save=pre_save)
    return view.run()

    

@decorators.require_login
@expose('/selfdevs/<id>/delete')
def selfdevs_delete(id):
    from uliweb.utils.generic import DeleteView
    from uliweb.orm import get_model
    
    Selfdevs = get_model('selfdev')
    obj = Selfdevs.get(Selfdevs.c.system == int(id))
    
    view = DeleteView('selfdev', ok_url='/systems/%s' % obj.system.id, obj=obj)
    return view.run()

@decorators.require_login
@expose('/selfdev_zxs/<id>/view')
def selfdev_zx_view(id):
    from uliweb.orm import get_model
        
    Kfzx = get_model('kfzx')
    obj = Kfzx.get(int(id))
    Selfdev_zx = get_model('selfdev_zx')
    objs = Selfdev_zx.get(Selfdev_zx.c.kfzx == int(id))
    
    if not objs:
        objs = Selfdev_zx(kfzx=obj)
        objs.save()
    
    return {'objects':objs}

@decorators.require_login
@expose('/selfdev_zxs/<id>/edit')
def selfdev_zx_edit(id):
    from uliweb.utils.generic import EditView
    from uliweb.orm import get_model
    from uliweb.form import DateField
    from plugs.ui.jquery.jquery.widgets import DatePicker
    from datetime import datetime, date
    from uliweb.form.template_layout import TemplateLayout
    
    Kfzx = get_model('kfzx')
    obj = Kfzx.get(int(id))
    Selfdev_zx = get_model('selfdev_zx')
    objs = Selfdev_zx.get(Selfdev_zx.c.kfzx == int(id))
    
    data = {}
    data['kfzx'] = objs.kfzx.name
    
    static_fields = ('kfzx')
    fields = ('kfzx', 'initial', 'lastyear', 'thisyear', 'destination')
    
    def post_created_form(fcls, model, objs):
        fcls.layout_class = TemplateLayout
        fcls.layout = 'layout_selfdev_zx_edit.html'
        
    def pre_save(objs, data):
        del data['kfzx']
        data['kfzx'] = objs.kfzx 
    
    view = EditView(Selfdev_zx, ok_url='/config/kfzx/%s' % id, obj=objs, data=data,
        post_created_form=post_created_form, fields=fields, 
        static_fields=static_fields, pre_save=pre_save)
    return view.run()
    

@decorators.require_login
@expose('/selfdevs/selfdev_if/modified')
def selfdevs_selfdev_if_modified():
    from uliweb.orm import get_model,do_
    from uliweb import request
    
    Selfdev_if_details = get_model('selfdev_if_details')
    Requirement = get_model('requirement')
    Tasks_develop = get_model('tasks_develop')
    System = get_model('system')
    data={}
    data['msg']=''
    data['selfdev_if']=''
    data['selfdev_modified']= ''
    #处理来自准开发需求的设置自住率考核标志命令
    if request.values.get('resource') == 'requirement':
        id = int(request.values.get('object_id'))
        obj=Requirement.get(Requirement.c.id==id)
        if obj:
            selfdev_if = not obj.selfdev_if
            obj.selfdev_if = selfdev_if
            obj.save()
            data['msg']=u'自主率考核标志修改成功！'
            if selfdev_if:
                data['selfdev_modified']=u'不参与系统自主率考核'
            else:
                data['selfdev_modified']=u'参与系统自主率考核'
            if selfdev_if:
                data['selfdev_if']=u'是'
            else:
                data['selfdev_if']=u'否'
            
            do_(Tasks_develop.table.update(Tasks_develop.c.requirement==id).values(selfdev_if = selfdev_if))
            #插入修改明细
            row = Selfdev_if_details(requirement=id , system=None , task=None , user=request.user.id , selfdev_if=selfdev_if)
            row.save()
        else:
            data['msg']=u'自主率考核标志修改失败！'    
    #处理来自任务的设置自住率考核标志命令
    if request.values.get('resource') == 'tasks_develop':
        id = int(request.values.get('object_id'))
        obj=Tasks_develop.get(Tasks_develop.c.id==id)
        if obj:
            selfdev_if = not obj.selfdev_if
            obj.selfdev_if = selfdev_if
            obj.save()
            data['msg']=u'自主率考核标志修改成功！'
            if selfdev_if:
                data['selfdev_modified']=u'不参与系统自主率考核'
            else:
                data['selfdev_modified']=u'参与系统自主率考核'
            if selfdev_if:
                data['selfdev_if']=u'是'
            else:
                data['selfdev_if']=u'否'
            
            #插入修改明细
            row = Selfdev_if_details(requirement=None , system=None , task=id , user=request.user.id , selfdev_if=selfdev_if)
            row.save()
            
        else:
            data['msg']=u'自主率考核标志修改失败！'
    #处理来自系统的设置自住率考核标志命令
    if request.values.get('resource') == 'system':
        id = int(request.values.get('object_id'))
        obj=System.get(System.c.id==id)
        if obj:
            selfdev_if = not obj.selfdev_if
            obj.selfdev_if = selfdev_if
            obj.save()
            data['msg']=u'自主率考核标志修改成功！'
            if selfdev_if:
                data['selfdev_modified']=u'不参与系统自主率考核'
            else:
                data['selfdev_modified']=u'参与系统自主率考核'
            if selfdev_if:
                data['selfdev_if']=u'是'
            else:
                data['selfdev_if']=u'否'
            
            #插入修改明细
            row = Selfdev_if_details(requirement=None , system=id , task=None , user=request.user.id , selfdev_if=selfdev_if)
            row.save()
            
        else:
            data['msg']=u'自主率考核标志修改失败！'
        
    return json(data)

@decorators.require_login
@expose('/selfdevs/selfdev_if/query')
def selfdevs_selfdev_if_query():
    from uliweb.orm import get_model,do_
    from uliweb import request
    from uliweb.utils.generic import ListView
    import generic.views as g_views
    from query import create_Selfdev_if_details_query
    
    Selfdev_if_details = get_model('selfdev_if_details')
    Requirement = get_model('requirement')
    Task = get_model('tasks_develop')
    query = create_Selfdev_if_details_query(url_for(selfdevs_selfdev_if_query))
    c = query.run()
    
    condition = None
    #   if (request.user.is_gm)and(request.user.department=='2'):
    #       c['kfzx'] = request.user._kfzx_
    if c.get('system'):
        condition = (Selfdev_if_details.c.system == int(c['system'])) & condition
#    if c.get('requirement'):
#        condition = (Selfdev_if_details.c.requirement == int(c['requirement'])) & condition
#    if c.get('task'):
#        condition = (Selfdev_if_details.c.tasks_develop == int(c['task'])) & condition
    if c.get('requirement_title'):
            _con = Requirement.c.title.like('%'+c['requirement_title']+'%') | Requirement.c.req_id.like('%'+c['requirement_title']+'%')
            _r = Requirement.filter(_con).values('id')
            req_ids = [x[0] for x in _r]
            if req_ids:
                condition = (Selfdev_if_details.c.requirement.in_(req_ids)) & condition
            else:
#                condition = (Selfdev_if_details.model.c.id != Selfdev_if_details.model.c.id) & condition
                condition = (False) & condition
    
    if c.get('task_title'):
        _r = Task.filter(Task.c.name.like('%'+c['task_title']+'%') | Task.c.task_id.like('%'+c['task_title']+'%')).values('id')
        task_ids = [x[0] for x in _r]
        if task_ids:
            condition = (Selfdev_if_details.model.c.task.in_(task_ids)) & condition
        else:
#            condition = (Selfdev_if_details.model.c.id != Selfdev_if_details.model.c.id) & condition
            condition = (False) & condition
    
    if c.get('begin_date'):
        condition = (WeekReport.c.created_date >= c['begin_date']) & condition
    if c.get('end_date'):
        condition = (WeekReport.c.created_date <= c['end_date']) & condition
    def selfdev_if(value, obj):
        if obj.selfdev_if:
            return u'是'
        else:
            return u'否'
    
    fields_convert_map = {'selfdev_if':selfdev_if}
    pageno = int(request.values.get('page', 1)) - 1
    rows_per_page=int(request.values.get('rows', settings.get_var('PARA/ROWS_PER_PAGE', 10)))
    
    view =  ListView(Selfdev_if_details, condition=condition,
        rows_per_page=rows_per_page, 
        pageno=pageno, 
        id='tasks_table', fields_convert_map=fields_convert_map)
    
    if 'download' in request.GET:
        return view.download('selfdevs_selfdev_if_query.xls', action='download', timeout=0,
    #       return view.download('%s.csv' % task_type, action='download', timeout=0,
            )
    else:
        data = {'query_form':query.form}        
        return g_views.generic_list('selfdev_if_details', '/selfdevs/selfdev_if/query',
            template='selfdevs_selfdev_if_query.html', view=view, data=data, json_result=True)
    